package cn.xianshu.shopping_cart.service.impl;

import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.service.impl.BasicServiceImpl;
import cn.xianshu.pet.domain.Pet;
import cn.xianshu.pet.mapper.PetMapper;
import cn.xianshu.shopping_cart.controller.ShoppingCartController;
import cn.xianshu.shopping_cart.domain.ShoppingCart;
import cn.xianshu.shopping_cart.mapper.ShoppingCartMapper;
import cn.xianshu.shopping_cart.service.IShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;



@Service
@Transactional
public class ShoppingCartServiceImpl extends BasicServiceImpl<ShoppingCartController> implements IShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 加入购物车
     * @param id
     */
    @Override
    public void addcart(Long id) {
        if (id == null){
            throw new BasicException("无效的商品id!");
        }

        Date date = new Date();
        ShoppingCart cart = new ShoppingCart();
        // 查询宠物信息
        Pet pet = petMapper.loadById(id);

        if (pet == null){
            throw new BasicException("无效的宠物信息");
        }

        cart.setAddTime(date);
        // 将宠物信息id保存到购物车
        shoppingCartMapper.addId(pet.getId());
    }

    /**
     * 展示数据  没完成
     * @retur
     */
    @Override
    public ShoppingCart cartdata() {

        // Object o = redisTemplate.opsForValue().get();
        ShoppingCart cart = shoppingCartMapper.loadByAll();
        if (cart.getState() != 1){
        }
        return cart;
    }


    /**
     * 购物车删除商品
     * @param id
     */
    @Override
    public void remove(Long id) {
        ShoppingCart cart = shoppingCartMapper.loadById(id);
        cart.setState(ShoppingCart.CART_STATE);
        shoppingCartMapper.update(cart);
    }


    /**
     * 金额计算  没完成
     * @param ids
     * @return
     */
    @Override
    public List<ShoppingCart> cal(List<Long> ids) {
        List<ShoppingCart> carts = shoppingCartMapper.loadByCal(ids);
        return null;
    }
}
